package cn.bdqn.service;




import cn.bdqn.dao1.*;
import cn.bdqn.entity.AuthorityRole1;
import cn.bdqn.entity.AuthorityUser1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: springbootSecurity
 * @Package: cn.bdqn.utile
 * @Author: huat
 * @Date: 2019/12/12 14:48
 * @Version: 1.0
 */
@Service//将这个类注入到spring容器中
public class UserService1Impl implements UserService1 {

    @Autowired
    private UserDao1 userDao1;//获取用户
    @Autowired
    private RoleDao1 roleDao1;//获取角色
    @Autowired
    private MenuDao1 menuDao1;//获取路径集合
    @Autowired
    private PasswordEncoder myPasswordEncoder; //使用springSecurity加密规则




    //@Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       try{
           //根据用户名查询用户
           AuthorityUser1 authorityUser =userDao1.getUser(username);
           if(null== authorityUser){
               //返回null代表验证失败
               return null;
           }
           //放角色的集合
           List<SimpleGrantedAuthority> authorities=new ArrayList<SimpleGrantedAuthority>();
           List<AuthorityRole1> roleList=roleDao1.getAllRole(authorityUser.getId());
           //list中放入角色权限
          if(null!=roleList&&roleList.size()>0){
              for (AuthorityRole1 role:roleList) {
                  authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
              }
          }
           //{noop}代表使用明文验证
         // UserDetails userDetails=new User(authorityUser.getUsername(),"{noop}"+authorityUser.getPassword(),authorities);
           //使用密文验证
           UserDetails userDetails=new User(authorityUser.getUsername(),authorityUser.getPassword(),authorities);
          return userDetails;
       }catch (Exception e){
           e.printStackTrace();
           //返回null代表验证失败
           return null;
       }

    }

    public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        //passwordEncoder.encode("123456")加密方法
        //执行多次加密后的结果均不一样
        //执行第一次:$2a$10$RSlzzO4xAubBLZMB8jPL8eMFX17gM61JfrgINpzOiaEsBdtKBuSDe
        //执行第二次:$2a$10$2uQWdP9qAXGK4H0um0gtG.o1QzbGhBlbxx8YjX526jIrVBNKyziFe
        //执行第三次:$2a$10$8CWttAISfamloSErsqEAPemALyZvj8VWaUCBJKcm2GE1dhFR.7oYG
        //security加密方式是动态加盐
        System.out.println(passwordEncoder.encode("123456"));
        //比较明文和密文是否一致
        System.out.println(passwordEncoder.matches("123456","$2a$10$lstWyJ6NlbdZ2uVr8ncsHOUKsG0VAT4FzDdy0gqy6gbaOqpp6ouDq"));
    }


}
